package cyapps.config;

import java.awt.BorderLayout;
import java.util.Map;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 * @author CyApps
 */
public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTree             tree             = new JTree();
	ConfigFile                file;
	
	public MainFrame(ConfigFile file) {
		this.file = file;
		this.setBounds(600, 300, 400, 400);
		this.updateTree();
		getContentPane().add(tree, BorderLayout.WEST);
	}
	public void updateTree() {
		final Map<String, Map<String, String>> configs = file.getConfigs();
		final Set<String> keySet = configs.keySet();
		tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(file.getFileName()) {
			private static final long serialVersionUID = 1L;
			{
				DefaultMutableTreeNode node;
				for (String string : keySet) {
					node = new DefaultMutableTreeNode(string);
					add(node);
					Map<String, String> subMap = configs.get(string);
					final Set<String> subKeySet = subMap.keySet();
					DefaultMutableTreeNode subNode;
					for (String subString : subKeySet) {
						subNode = new DefaultMutableTreeNode(subString);
						node.add(subNode);
					}
				}
			}
		}));
	}
	@SuppressWarnings("unused")
    private void setFile(ConfigFile file) {
		this.file = file;
	}
}
